﻿using ConfExpert.Forms.Base;
using DevExpress.XtraEditors;
using DevTeam;


using System;
using System.Data;

namespace ConfExpert.Forms.SatinAlma.YeniYapi.Confirmation
{
    public partial class DuzeltK : XForm
    {
        public DuzeltK()
        {
            InitializeComponent();
        }

        private void DuzeltK_Load(object sender, EventArgs e)
        {
            gridView1.Init("DuzeltK", "");
            gridView1.AddDetailGrid(gridView2, "POLinesId");

            gridView1.AddColumn("OranY", typeof(decimal), "YuklenenMt/KonfMt");
            gridView1.AddColumn("OranS", typeof(decimal), "SipMt/KonfMt");

            gridView1.AddColumn("Adet", typeof(decimal), "COUNT(Child({FK}).KonfId)");

            gridView1.FormatAsNumber(2, "OranY", "OranS");
            gridView1.FormatAsNumber(1, "KonfMt", "YuklenenMt", "SipMt");
            gridView1.AddSummary("KonfMt", "YuklenenMt", "SipMt");

            gridView2.FormatAsNumber(1, "Qty", "KonfMt", "ToplamYuklenen");
            gridView2.AddSummary("Qty", "KonfMt", "ToplamYuklenen");
            gridView1.BestFitColumns();
            gridView2.BestFitColumns();

            gridView1.FocusedRowChanged += gridView1_FocusedRowChanged;

            btnKaydet.ItemClick += btnKaydet_ItemClick;
            btnToplu.ItemClick += btnToplu_ItemClick;
        }

        void btnToplu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            string pols = gridView1.GetSelectedValues("POLinesId");
            foreach(string str in pols.Split(",".ToCharArray()))
            {
                gridView1.FocusedRowHandle = gridView1.LocateByDisplayText(0, gridView1.Columns["POLinesId"], str);
                gridView2.SelectAll();
                btnKaydet.PerformClick();
            }
        }

        void btnKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DataTable d = new DataTable("Merge");
            d.Columns.Add("AyrimlarId", typeof(int));
            d.Columns.Add("Qty", typeof(decimal));
            d.Columns.Add("KonfId", typeof(int));
            d.Columns.Add("KonfMt", typeof(decimal));

            gridView1.EndEditing();
            gridView2.EndEditing();

            foreach (int rowHandle in gridView2.GetSelectedRows())
                if (rowHandle > -1)
                    if (DevExpress.XtraGrid.GridControl.NewItemRowHandle != gridView2.FocusedRowHandle &&
                        DevExpress.XtraGrid.GridControl.AutoFilterRowHandle != gridView2.FocusedRowHandle)
                        d.Rows.Add(gridView2.GetRowCellValue(rowHandle, "AyrimlarId"),
                            gridView2.GetRowCellValue(rowHandle, "Qty"),
                            gridView2.GetRowCellValue(rowHandle, "KonfId"),
                            gridView2.GetRowCellValue(rowHandle, "KonfMt")
                            );

            string POLinesId = string.Empty;
            DataRow r = gridView1.GetFocusedDataRow();
            if(r!=null)
                POLinesId = r["POLinesId"].ToStr();

            decimal q = d.Compute("SUM(Qty)", string.Empty).ToDecimal();
            decimal k = d.Compute("SUM(KonfMt)", string.Empty).ToDecimal();
            if (q != k)
            {
                XtraMessageBox.Show("Seçilen satırların KonfMt ve AyrılanMt toplamları eşit olmalı!", POLinesId);
                return;
            }

            int c = d.Compute("COUNT(KonfId)", "KonfId>0").ToInt();
            int a = d.Compute("COUNT(AyrimlarId)", "AyrimlarId>0").ToInt();
            if (!(c == 1 && a > 1))
            {
                XtraMessageBox.Show("Bölünecek Konf. sayısı 1, ayrımların sayısı da 1'den büyük olmalı!", POLinesId);
                return;
            }

            int neg = d.Compute("SUM(Qty)", "Qty<0").ToInt();
            if (neg<0)
            {
                XtraMessageBox.Show("Qty<0 olan ayrım olamaz!", POLinesId);
                return;
            }

            ExecuteNonQuery("DuzeltK", d.Serialize());
            Notify(POLinesId);
            /*
            gridView1.VeriyiGuncelle(true, "DuzeltK", d.Serialize());
            gridView1.BestFitColumns();
            gridView2.BestFitColumns();
             */
        }

        void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            gridView2.BestFitColumns();
        }
    }
}
